CONFIG.COLUMN_CODE = "ggly_history";
CONFIG.CURRENT_URI = "column/history/history" + CONFIG.EXT;

var History = {
	userid: Epg.cookie.get("userid"),								// 当前用户ID
	role: Epg.cookie.get("role"),									// 当前用户角色
	spriteType: Epg.cookie.get('spriteType', 'rabbit'),
	currentPage: Epg.getParamInt("currentPage", "1"),				// 当前第几页
	dataList: "",													// 查询到的剧集信息
	pageCount: 0,													// 剧集总页数
	
	back: function(){
		location.href = Epg.getBackURI();
	},
	seriesFocus: function(){										// 剧集获得焦点
		//G(Epg.btn.current.id+"_txt").style.backgroundColor = "rgba(73, 179, 241, 0.9)";
		Epg.marquee.start();										// 剧集名称开始滚动
	},
	seriesBlur: function(){											// 剧集失去焦点
		//G(Epg.btn.previous.id+"_txt").style.backgroundColor = "rgba(0, 0, 0, 0.5)";
		Epg.marquee.stop();											// 剧集名称停止滚动
	},
	goSubSeries: function() {
		var index = Epg.btn.current.index;
		CONFIG.CURRENT_URI += '?currentPage=' + History.currentPage;
		Epg.go("column/subSeries/subSeries"+CONFIG.EXT+"?code="+History.dataList[index].code);
	},
	collectAction: function() {
		Epg.cookie.set("back_uri_list", '[]');
		location.href = CONFIG.EPG_PATH + 'column/iptv_home/iptv_home' + CONFIG.EXT;
	},
	showSeriesData: function(dataList, container, prev){		// 显示剧集信息
		var html = "";
		dataList.forEach(function(series, index){
			html += '<div class="common_series_item" style="background-image:url('+CONFIG.EPG_PATH+series.thumbHD+'); overflow:visible;">'+
				'<div id="'+prev+index+'_txt" class="txt" style="overflow:hidden;">'+series.name+'</div>';
			html += '<div id="'+prev+index+'"></div>';
			if (dataList[index].score != null && dataList[index].score != '') {
				html += '<div class="butttomSeriesScore"><div class="butttomSeriesScore_score">'+dataList[index].score+'</div></div>';
			}
			if (dataList[index].onlineDate != null && ((new Date().getTime()- dataList[index].onlineDate)<CONFIG.SHOW_SERIES_ONLINEDATE)) {
				html += '<div class="seriesIsNew"></div>';
			}
			html += '</div>';
		});
		G(container).innerHTML = html;
	},
	playVideo: function(flag) {									// 语音播放方法
		Epg.Mp.destroy();
		Epg.Mp.init();
		Epg.Mp.fullscreenPlay(CONFIG.EPG_PATH+'column/history/media/'+History.spriteType+'/'+flag+'_history.mp3');
	},
	init: function(){					
		var self = this;
		var postData = {"userid":History.userid, "role":History.role, "current":History.currentPage, "pageSize":10};
		Epg.postAPI("record/get-watch-record-list", postData, function(data){
			if(data && data.code === 0 && data.pb.dataList && data.pb.dataList.length > 0){
				self.dataList = data.pb.dataList;											// 当前页信息
				var pageCount = data.pb.pageCount;											// 总页数
				self.currentPage > 1 ? S("top_arrow") : H("top_arrow");						// 如果还有上一页
				self.currentPage < pageCount ? S("bottom_arrow") : H("bottom_arrow");		// 如果还有下一页
				var containers = ["prev", "current", "next"];								// 当前选择哪个容器
				G(containers[(self.currentPage-1) % 3] + "_column_wrapper").style.top = "-720px";	// 前一页模块
				G(containers[self.currentPage % 3] + "_column_wrapper").style.top = "0px";			// 当前页模块
				G(containers[(self.currentPage+1) % 3] + "_column_wrapper").style.top = "720px";	// 下一页模块
				self.showSeriesData(self.dataList, containers[self.currentPage%3]+"_column", containers[self.currentPage%3]+"Series_");			// 显示当前页信息
				preloading = new Preloading(self.currentPage, pageCount, 10);				// 初始化预加载对象
				preloading.action[self.currentPage] = self.dataList;						// 保存当前页的信息
				preloading.addData("up");													// 加载上一页信息
				preloading.addData("down");													// 加载下一页信息
				Epg.btn.init([Epg.getParam('f'), containers[self.currentPage%3]+"Series_0"], buttons, "", false);
				
				if('true' === Epg.getParam('isPlay', 'false')) {// 从别的页面进入才播放语音
					History.playVideo('yes');					// 语音播放方法
				}
			}else {
				S("collect");
				Show("no_data_tip");
				if('true' === Epg.getParam('isPlay', 'false')) {// 从别的页面进入才播放语音
					History.playVideo('no');					// 语音播放方法 
				}
				Epg.btn.init('collect_focus', buttons, "", false);
			}
		});
	}
};

window.onunload = function() {
	Epg.Mp.destroy();
}

var buttons = [
    {id:'seriesPrev', focusHandler:pageGo},
    {id:'seriesNext', focusHandler:pageGo},
    {id:'collect_focus', action:History.collectAction,focusClass:'collect'},
    
    {id:'prevSeries_0', index:0, action:History.goSubSeries, up:'seriesPrev', down:'prevSeries_5', right:'prevSeries_1', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_1', index:1, action:History.goSubSeries, up:'seriesPrev', down:'prevSeries_6', left:'prevSeries_0', right:'prevSeries_2', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_2', index:2, action:History.goSubSeries, up:'seriesPrev', down:'prevSeries_7', left:'prevSeries_1', right:'prevSeries_3', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_3', index:3, action:History.goSubSeries, up:'seriesPrev', down:'prevSeries_8', left:'prevSeries_2', right:'prevSeries_4', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_4', index:4, action:History.goSubSeries, up:'seriesPrev', down:'prevSeries_9', left:'prevSeries_3', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},    
    {id:'prevSeries_5', index:5, action:History.goSubSeries, up:'prevSeries_0', down:'seriesNext', right:'prevSeries_6', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_6', index:6, action:History.goSubSeries, up:'prevSeries_1', down:'seriesNext', left:'prevSeries_5', right:'prevSeries_7', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_7', index:7, action:History.goSubSeries, up:'prevSeries_2', down:'seriesNext', left:'prevSeries_6', right:'prevSeries_8', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_8', index:8, action:History.goSubSeries, up:'prevSeries_3', down:'seriesNext', left:'prevSeries_7', right:'prevSeries_9', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'prevSeries_9', index:9, action:History.goSubSeries, up:'prevSeries_4', down:'seriesNext', left:'prevSeries_8', right:'seriesNext', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    
    {id:'currentSeries_0', index:0, action:History.goSubSeries, up:'seriesPrev', down:'currentSeries_5', right:'currentSeries_1', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_1', index:1, action:History.goSubSeries, up:'seriesPrev', down:['currentSeries_6','currentSeries_5'], left:'currentSeries_0', right:'currentSeries_2', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_2', index:2, action:History.goSubSeries, up:'seriesPrev', down:['currentSeries_7','currentSeries_6','currentSeries_5'], left:'currentSeries_1', right:'currentSeries_3', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_3', index:3, action:History.goSubSeries, up:'seriesPrev', down:['currentSeries_8','currentSeries_7','currentSeries_6','currentSeries_5'], left:'currentSeries_2', right:'currentSeries_4', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_4', index:4, action:History.goSubSeries, up:'seriesPrev', down:['currentSeries_9','currentSeries_8','currentSeries_7','currentSeries_6','currentSeries_5'], left:'currentSeries_3', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},    
    {id:'currentSeries_5', index:5, action:History.goSubSeries, up:'currentSeries_0', down:'seriesNext', right:'currentSeries_6', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_6', index:6, action:History.goSubSeries, up:'currentSeries_1', down:'seriesNext', left:'currentSeries_5', right:'currentSeries_7', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_7', index:7, action:History.goSubSeries, up:'currentSeries_2', down:'seriesNext', left:'currentSeries_6', right:'currentSeries_8', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_8', index:8, action:History.goSubSeries, up:'currentSeries_3', down:'seriesNext', left:'currentSeries_7', right:'currentSeries_9', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'currentSeries_9', index:9, action:History.goSubSeries, up:'currentSeries_4', down:'seriesNext', left:'currentSeries_8', right:'seriesNext', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    
    {id:'nextSeries_0', index:0, action:History.goSubSeries, up:'seriesPrev', down:'nextSeries_5', right:'nextSeries_1', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_1', index:1, action:History.goSubSeries, up:'seriesPrev', down:'nextSeries_6', left:'nextSeries_0', right:'nextSeries_2', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_2', index:2, action:History.goSubSeries, up:'seriesPrev', down:'nextSeries_7', left:'nextSeries_1', right:'nextSeries_3', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_3', index:3, action:History.goSubSeries, up:'seriesPrev', down:'nextSeries_8', left:'nextSeries_2', right:'nextSeries_4', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_4', index:4, action:History.goSubSeries, up:'seriesPrev', down:'nextSeries_9', left:'nextSeries_3', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},    
    {id:'nextSeries_5', index:5, action:History.goSubSeries, up:'nextSeries_0', down:'seriesNext', right:'nextSeries_6', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_6', index:6, action:History.goSubSeries, up:'nextSeries_1', down:'seriesNext', left:'nextSeries_5', right:'nextSeries_7', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_7', index:7, action:History.goSubSeries, up:'nextSeries_2', down:'seriesNext', left:'nextSeries_6', right:'nextSeries_8', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_8', index:8, action:History.goSubSeries, up:'nextSeries_3', down:'seriesNext', left:'nextSeries_7', right:'nextSeries_9', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur},
    {id:'nextSeries_9', index:9, action:History.goSubSeries, up:'nextSeries_4', down:'seriesNext', left:'nextSeries_8', right:'seriesNext', focusClass:'series', focusHandler:History.seriesFocus, blurHandler:History.seriesBlur}
];

;(function(){
	Epg.key.init();
	Epg.key.set({
		KEY_ENTER: 'Epg.Button.click()',			//确定键
		KEY_LEFT: 'Epg.Button.move("left")',		//左键
		KEY_RIGHT: 'Epg.Button.move("right")',		//右键
		KEY_UP: 'Epg.Button.move("up")',			//上键
		KEY_DOWN: 'Epg.Button.move("down")',		//下键
		KEY_1: 'test()',
		KEY_BACK: History.back						//返回键
	});
	History.init();
})();

function test(){
	location.href = CONFIG.EPG_PATH+'activity/20180905_wwdklty_activity/index.jsp';
}

var preloading;
function pageGo(button){
	var previous = Epg.btn.previous.id;
	preloading.setPrevious(previous);
	if(button.id === "seriesPrev"){
		if(preloading.currentPage > 1){							// 如果当前页大于1, 可以往上滚
			preloading.getAction("up");
		}else {
			Epg.btn.set(previous);
		}
	}else if(button.id === "seriesNext"){
		if(preloading.currentPage < preloading.pageCount){		// 如果当前页小于总页数, 可以往下滚
			preloading.getAction("down");
		}else {
			Epg.btn.set(previous);								// 大于等于总页数还回之前选中的按钮
		}
	}
}

var Preloading = function(currentPage, pageCount, pageSize){
	this.currentPage = currentPage;
	this.pageCount = pageCount;
	this.pageSize = pageSize;
	this.action = {};
	this.containers = ["prev", "current", "next"];	// 当前选择哪个容器
	this.isMove = false;							// 标识是否可以切换光标
	this.previous = "";								// 保存切屏前的光标ID
};
Preloading.prototype = {
	setPrevious: function(previous){
		this.previous = previous;
	},
	addData: function(dir){
		var _this = this, flag = false;
		var isAddData = dir === "down" ? (this.currentPage < this.pageCount) : (this.currentPage > 1); // 判断当前页是否大于1或者小于总页数
		if(isAddData){
			var current = dir === "down" ? (this.currentPage + 1) : (this.currentPage - 1);			// 如果向下当前页加1或者减1
			for(var i in this.action){
				if(current === i){										// 如果加载了要加载的页码数据
					flag = true;
					break;
				}
			}
			if(flag === false){											// 如果没有加载过
				var postData = {"userid":History.userid, "role":History.role, "current":current, "pageSize":this.pageSize};
				Epg.postAPI('record/get-watch-record-list', postData, function(data){
					if(data && data.code === 0){
						_this.action[current] = data.pb.dataList;			// 保存该页信息
						var container = _this.containers[current % 3];		// 用哪个容器
						History.showSeriesData(_this.action[current], container+"_column", container+"Series_");	// 显示该页信息 
					}
				});
			}else {														// 如果加载过
				var container = _this.containers[current % 3];
				History.showSeriesData(_this.action[current], container+"_column", container+"Series_");
			}
		}
	},
	getAction: function(dir){
		var _this = this;
		var prev = dir === "down" ? (this.currentPage - 1) % 3 : (this.currentPage + 1) % 3;		// 向下滚获得前一页页码,否则后一页页码
		var next = dir === "down" ? (this.currentPage + 1) % 3 : (this.currentPage - 1) % 3;		// 向下滚获得后一页页码,否则前一页页码
		G(this.containers[prev]+"_column_wrapper").style.top = dir === "down" ? "720px" : "-720px";	// 如果向下滚,第一页移到最后,否则最后一页移到最前
		this.move(_this.containers[_this.currentPage%3]+"_column_wrapper", dir === "down" ? -720 : 720, function(){
			_this.currentPage = dir === "down" ? (_this.currentPage + 1) : (_this.currentPage - 1);
			History.currentPage = _this.currentPage;
			History.dataList = _this.action[_this.currentPage];
			_this.currentPage > 1 ? S("top_arrow") : H("top_arrow");								// 如果还有上一页
			_this.currentPage < _this.pageCount ? S("bottom_arrow") : H("bottom_arrow");			// 如果还有下一页
			var index = parseInt(Epg.btn.get(_this.previous).index);								// 当前是第几块
			for(var i = index; i >= 0; i--){
				if(G(_this.containers[_this.currentPage%3]+"Series_"+i)){							// 如果下一页第几块存在
					Epg.btn.set(_this.containers[_this.currentPage%3]+"Series_"+i);					// 选中
					break;
				}
			}
			_this.addData(dir);
		});
		this.move(_this.containers[next] + "_column_wrapper", 0);
	},	
	move: function(source, target, callback){
		var _this = this;
		this.isMove = true;
		G(source).style.top = target + "px";
		callback && callback();
		this.isMove = false;
	}
};
